package com.leemz.spring.factory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 简易工厂类
 * @author leemz
 */
public class BeanFactory {

    private static Properties env = new Properties();
    static {
        try {
            env.load(BeanFactory.class.getResourceAsStream("/applicationContext.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 直接在工厂中new需要的对象，业务代码虽然解耦了，
     * 但是耦合放在了工厂里。
     * @return
     */
    /*public static UserService getBean() {
        return new UserServiceImpl();
    }*/

    /**
     * 使用反射获取需要的bean，需要进行类型转换，使用类的全限定名来反射获取
     * 需要替换实现类时依然需要更改工厂源码
     * @return
     */
    /*public static UserService getUserService() {
        UserService userService = null;
        try {
            Class cls = Class.forName("com.leemz.spring.factory.UserServiceImpl");
            userService = (UserService) cls.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return userService;
    }*/

    /**
     * 使用反射全限定包名+类名的形式来获取bean，但是将要创建的bean的全限定名
     * 转移到配置文件中。
     * @return
     */
    /*public static UserService getUserService() {
        UserService userService = null;
        try {
            Class cls = Class.forName(env.getProperty("userService"));
            userService = (UserService) cls.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return userService;
    }*/

    /**
     * 和获取service一样获取到的实现类
     * @return
     */
    /*public static UserDao getUserDao() {
        UserDao userDao = null;
        try {
            Class cls = Class.forName(env.getProperty("userDao"));
            userDao = (UserDao) cls.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return userDao;
    }*/

    /**
     * 由于获取userService和获取userDao存在大量的重复代码，
     * 做一个代码提取，提取到一个公共方法中
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        Object object = null;
        try {
            Class cls = Class.forName(env.getProperty(beanName));
            object = cls.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return object;
    }

}
